package com.infore.quartz.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;

@EnableCaching
//@EnableEurekaClient
@EnableDiscoveryClient
@EnableSwagger2
@MapperScan({"com.infore.quartz.main.dao"})
@SpringBootApplication(scanBasePackages={"com.infore"})
@EnableFeignClients(basePackages = {"com.infore"})
@ComponentScan(basePackages = {"com.infore"})
public class QuartzApplication extends SpringBootServletInitializer  implements CommandLineRunner {
        protected static Log log = LogFactory.getLog(QuartzApplication.class);
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(QuartzApplication.class);
        }

        public static void main(String[] args) throws UnknownHostException {
            ConfigurableApplicationContext application = SpringApplication.run(QuartzApplication.class, args);
            Environment env = application.getEnvironment();
            String ip = InetAddress.getLocalHost().getHostAddress();
            String port = env.getProperty("server.port");
            String path = env.getProperty("server.servlet.context-path")==null? "":env.getProperty("server.servlet.context-path");
            log.info("\n----------------------------------------------------------\n" +
                    "Application service is running! Access URLs:\n" +
                    "Local请求地址: \thttp://localhost:" + port + path + "/JobManager.html\n" +
                    "External请求地址: \thttp://" + ip + ":" + port + path + "/JobManager.html\n" +
                    "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n" +
                    "----------------------------------------------------------");
        }
        @Override
        public void run(String... args) throws Exception {
            System.out.println("-------------Quartz service started successfully----------------");
        }

}
